import pygame
import sys

from pygame import display, time, event, Surface, image, draw
from .character import *
from .network import *


class Game:
    def __init__(self, width: int, height: int, title: str, fps=60, map_size: tuple = (1248, 672)) -> None:
        self.title = title
        self.width = width
        self.height = height
        self.map_x = 0
        self.map_y = 0
        self.fps = fps

        self.map_data = [[0] * 40 for i in range(22)]

        self.map_size: tuple = map_size
        self.playerList: list[Role] = []
        self.__init_pygame()
        self.me = Player()
        # 注册玩家
        self.playerList.append(Role())

        Net.init()
        Net.isPlaying = True
        # threading.Thread(target=Net.sendMsg, args=(
        #     self.me,)).start()
        threading.Thread(target=Net.recvActions,
                         args=(self.playerList[0],)).start()
        self.update()

    def __init_pygame(self):
        pygame.init()
        display.set_caption(self.title)
        self.screen = display.set_mode((self.width, self.height))
        self.map_bottom = image.load(
            './dist/image/map_bottom.png').convert_alpha()
        self.map_top = image.load('./dist/image/map_top.png').convert_alpha()

        with open('./dist/image/map.map', 'r') as file:
            for y in range(0, 40):
                for x in range(0, 22):
                    t = file.readline()
                    v = int(t)
                    self.map_data[x][y] = v

        for x in range(0, 22):
            for y in range(0, 40):
                print(self.map_data[x][y], end=' ')
            print("")

        self.clock = pygame.time.Clock()

    # 事件处理
    def event_handler(self):
        for e in event.get():
            if e.type == pygame.QUIT:
                Net.isPlaying = False
                pygame.quit()
                print("游戏退出")
                sys.exit()
            self.me.eventHandler(e)

    # 屏幕刷新

    def update(self):
        while True:
            # 调用事件处理
            self.event_handler()
            self.me.move(self.map_data)
            # 绘制地图底层
            if self.me.x <= self.width / 2:
                self.map_x = 0
            elif self.me.x >= self.map_size[0] - self.width/2:
                self.map_x = -(self.map_size[0] - self.width)
            else:
                self.map_x = -(self.me.x - self.width/2)

            if self.me.y <= self.height/2:
                self.map_y = 0
            elif self.me.y >= self.map_size[1] - self.height/2:
                self.map_y = -(self.map_size[1]-self.height)
            else:
                self.map_y = -(self.me.y - self.height/2)

            self.screen.blit(self.map_bottom, (self.map_x, self.map_y))

            # 绘制人物
            for role in self.playerList:
                role.draw(self.screen, self.map_x+role.x, self.map_y+role.y)

            self.me.draw(self.screen, self.map_x +
                         self.me.x, self.map_y + self.me.y)

            # 绘制地图顶层
            self.screen.blit(self.map_top, (self.map_x, self.map_y))

            # test
            # for x in range(0, 22):
            #     for y in range(0, 40):
            #         if self.map_data[x][y] == 1:
            #             draw.rect(self.screen, (0, 0, 0),
            #                       (y*32+self.map_x, x*32+self.map_y, 32, 32), 0)

            # 刷新屏幕
            display.update()
            self.clock.tick(self.fps)
